import cv2
import numpy as np


# img = cv2.imread('‪F:\重广补注黄帝内经素问.二十四卷.唐.王冰注.明嘉靖二十九年顾从德覆宋刊本\重广补注黄帝内经素问.二十四卷.唐.王冰注.明嘉靖二十九年顾从德覆宋刊本_34.jpg')
path = 'I:\\bookget-1.2.1.windows-amd64\\www.loc.gov_2014514163\\0010.jp2'
img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1)
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化古籍黑白化

ret, dst = cv2.threshold(img2, 170, 255, cv2.THRESH_BINARY)
#ret,dst = cv2.threshold(img2,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU) #自动设置颜色范围
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
# kernel = np.ones((9,9),np.uint8)
dst2 = cv2.erode(dst,kernel,iterations=1)

# kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))


# dst2 = cv2.morphologyEx(dst,cv2.MORPH_CLOSE,kernel2)
# dst1 = cv2.morphologyEx(dst,cv2.MORPH_OPEN,kernel)
#kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
#dst3 = cv2.dilate(dst, kernel=kernel2, iterations=1)

# cv2.namedWindow('dts',cv2.WINDOW_FREERATIO)
cv2.namedWindow('img2', cv2.WINDOW_FREERATIO)
# cv2.imshow('img',img)
cv2.imshow('img2', dst2)
# cv2.imshow('dts',dst1)

# cv2.imwrite('m2.jpg', dst1)
cv2.waitKey(0)
